package com.groenlid.mobileapp;

import java.util.List;

import com.google.android.maps.*;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

import com.groenlid.mobileapp.UI.*;

public class Running extends MapActivity{
	
	private MapController mapController;
	private MapView mapView;
	private Running self = this;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.running);
    	
    	// MapView Settings
    	mapView = (MapView) findViewById(R.id.mapview);
    	mapView.setBuiltInZoomControls(true);
    	
    	// MapController Settings
    	mapController = mapView.getController();
    	mapController.setZoom(18);
    	
    	GeoUpdateHandler locationHandler = new GeoUpdateHandler();
    	
    	//Style the button
    	/*Button stop = (Button) findViewById(R.id.btnStopActivity);
    	stop.setShadowLayer(1, 1, 1, android.R.color.black);*/
    	
    	// LocationManager
    	LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    	locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationHandler);
    	
    }
    
    
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	public class GeoUpdateHandler implements LocationListener {

		public void onLocationChanged(Location location) {
			//Toast.makeText(self, "asd here", Toast.LENGTH_LONG).show();
			// Get Position
			int lat = (int) (location.getLatitude() * 1E6); // 40.747778 * 1E6 = 40747778;
			int lng = (int) (location.getLongitude() * 1E6);
			GeoPoint point = new GeoPoint(lat,lng);
			
			// Create Overlay
			List<Overlay> mapOverlays = mapView.getOverlays();
			Drawable drawable = self.getResources().getDrawable(R.drawable.pin);
			
			
			PositionOverlay overlay = new PositionOverlay(drawable, self);
			
			OverlayItem overlayitem = new OverlayItem(point, "hello", "Description");
			
			overlay.addOverlay(overlayitem);
			mapOverlays.clear();
			mapOverlays.add(overlay);
			
			// Move to new point
			mapController.animateTo(point);
		}

		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
			
		}

		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			
		}
		
	}
}
